
// Triple dropdowns are a MM/DD/YYYY dropdown selector
function tripleDropDown()
{
	jx.log( "TDD: Starting..." );
	
	this.daySelector = null;
	this.monthSelector = null;
	this.yearSelector = null;
	
	// Check dropdown as a possible day-of-month selector
	this.isDaySelector = function( elem )
	{
		var startIndex = 0;
		
		// Some age gates have "Day" or "Select" as the first option, attempt to detect and skip it
		while ( !jx.isNum( elem.options[ startIndex ].text ) )
		{
			startIndex++;
			
			// Couldn't find a numerical value
			if ( startIndex >= elem.options.length )
				return false;
		}
			
		// Starting at the first day of the month
		var dayCounter = 1;
			
		for ( var i = startIndex; i < elem.options.length; i++ )
		{
			// Check for days listed as "1", "2", "3" and "01", "02", "03", etc
			var typeA = "" + dayCounter;
			var typeB = ( dayCounter < 10 ) ? "0" + dayCounter : "" + dayCounter;
			
			if ( elem.options[ i ].text == typeA || elem.options[ i ].text == typeB )
			{
				dayCounter++;
			}
			else
			{
				return false;
			}
		}
		
		// Probably a month selector
		if ( dayCounter < 28 )
			return false;
		
		jx.log( "TDD: Found possible day selector" );
		
		return this.locateOtherSelectors( elem );
	}
	
	// Try to find the year and month selectors
	this.locateOtherSelectors = function( elem )
	{
		var monthSelect;
		var yearSelect;
		var yearIndex;
		
		// Find other sibling dropdowns
		var siblings = elem.parentNode.getElementsByTagName( "select" );
		
		// Again make sure we have at least 3
		if ( siblings.length < 3 )
		{
			siblings = [];
			
			// So we didn't find enough siblings from the same parent, try finding
			// some siblings that are horizontally lined up with us
			var elems = document.getElementsByTagName( "select" );
			for ( var i = 0; i < elems.length; i++ )
			{
				if( elems[ i ].offsetTop == elem.offsetTop )
					siblings.push( elems[ i ] );
			}
			
			// Still not enough?
			if ( siblings.length < 3 )
				return false;
		}
		
		// Cycle through each sibing
		for ( var i = 0; i < siblings.length; i++ )
		{
			var sib = siblings[ i ];
			
			// Don't process the one we already determined as the day-of-month selector
			if ( sib == elem )
				continue;
				
			// Most month selectors have between 12 and 14 options. 12 months + "Month" and "----", etc
			if ( sib.options.length >= 12 && sib.options.length <= 14 )
			{
				monthSelect = sib;
				jx.log( "TDD: Found possible month selector" );
				
				continue;
			}
			// Year selections normally have a lot of options, I chose 20 to be safe
			else if ( sib.options.length > 20 )
			{
				var check = 0;
				
				// Cycle through each one of the options
				for ( var j = 0; j < sib.options.length; j++ )
				{
					// Get the text of the option and check if its numeric
					var value = sib.options[ j ].text;
					if ( jx.isNum( value ) )
					{
						// The make sure its value is something we're looking for in a "year" value
						var num = parseInt( value );
						if ( num >= 1960 && num <= 2000 )
							check++;
						
						// FIXME: If we use the values from options, we won't need this
						if ( num < 1970 )
							yearIndex = j;
					}
				}
				
				// If enough of the values passed our checklist, lets assume its a year selector
				if ( check >= 40 )
				{
					yearSelect = sib;
					jx.log( "TDD: Found possible year selector" );
				}
				
				continue;
			}
		}
		
		// If we found both a possible year and month selector, lets attempt to use them
		if ( monthSelect && yearSelect )
		{
			this.use( elem, monthSelect, yearSelect, yearIndex );
			
			return true;
		}
		
		return false;
	}
	
	// Called when we attempt to use the dropdowns we found
	this.use = function( day, month, year, yearIndex )
	{
		jx.log( "TDD: Using key" );
		jx.usedKey();
		
		// FIXME: Use values from options
		day.selectedIndex = 2;
		month.selectedIndex = 2;
		year.selectedIndex = yearIndex;
	}
	
	// Find all the <select> elements in the document
	var elems = document.getElementsByTagName( "select" );

	// Anything less than 3 dropdowns isn't what we're looking for
	if ( elems.length >= 3 )
	{
		// Check each dropdown for the day-of-month selector
		for ( var i = 0; i < elems.length; i++ )
		{
			if( this.isDaySelector( elems[ i ] ) )
				break;
		}	
	}
	else
		jx.log( "TDD: Not enough dropdowns" );
	
	
	jx.log( "TDD: Complete" );
}

// Add to keyring
jx.addKey( tripleDropDown );
